package io.kiki.task.server.job.task.support.stop;

import akka.actor.ActorRef;
import io.kiki.task.common.core.enums.JobTaskTypeEnum;
import io.kiki.task.server.common.akka.ActorGenerator;
import io.kiki.task.server.common.util.ClientInfoUtils;
import io.kiki.task.server.job.task.dto.RealStopTaskInstanceDTO;
import io.kiki.task.server.job.task.support.JobTaskConverter;
import io.kiki.task.template.datasource.persistence.po.JobTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component

public class BroadcastTaskStopHandler extends AbstractJobTaskStopHandler {

    @Override
    public JobTaskTypeEnum getTaskType() {
        return JobTaskTypeEnum.BROADCAST;
    }

    @Override
    public void doStop(TaskStopJobContext context) {

        for (final JobTask jobTask : context.getJobTasks()) {
            RealStopTaskInstanceDTO taskInstanceDTO = JobTaskConverter.INSTANCE.toRealStopTaskInstanceDTO(context);
            taskInstanceDTO.setClientId(ClientInfoUtils.clientId(jobTask.getClientInfo()));

            ActorRef actorRef = ActorGenerator.jobRealStopTaskInstanceActor();
            actorRef.tell(taskInstanceDTO, actorRef);
        }

    }

}
